﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace RapidWPFDemo
{
    
    /// <summary>
    /// Interaction logic for ChangeTest.xaml
    /// </summary>
    public partial class ChangeTest : Window
    {
        ItemClass items = new ItemClass();
        public ChangeTest()
        {
            InitializeComponent();
            items.Add("one");
            items.Add("two");
            items.Add("three");
            items.Add("four");
            combobox.DataContext = items;
        }

        private const string small = "small";
        private const string large = "large large";

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            combobox.Measure(new Size(1000, 1000));
            Size ds = combobox.DesiredSize;
            if (button1.Content.Equals(small))
            {
                button1.Content = large;
                items.Add(large);
            }
            else
            {
                button1.Content = small;
                items.RemoveLast();
            }
            items.Item = items.Last();
        }
    }

    public class ItemClass : INotifyPropertyChanged
    {
        public ObservableCollection<string> items = new ObservableCollection<string>();
        private string item = string.Empty;
        public ObservableCollection<string> Items
        {
            get
            {
                return items;
            }
        }
        public string Item
        {
            get
            {
                return item;
            }
            set
            {
                item = (from i in items where i.Equals(value) select i).FirstOrDefault();
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Item"));
            }
        }
        public void Add(string item)
        {
            items.Add(item);
        }
        public void RemoveLast()
        {
            items.RemoveAt(items.Count - 1);
        }
        public string Last()
        {
            return items.Last();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
